<?php
class Users extends MY_Controller{

	function __construct()
	{
		parent::__construct();
		if(!$this->aauth->is_loggedin()){
			redirect('login');
		}
		$this->aauth->update_activity();
	}

	function index()
	{
		$data['title'] = $this->title('用户管理');
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/users/index');
		$this->load->view($this->theme.'/footer');
	}

	function resetAttempt ($user_id = false)
	{
		if(!$this->aauth->get_user($user_id)){
			redirect('users');
		}else{
			$this->aauth->reset_login_attempts($user_id);
			redirect('users');
		}
	}

	function addUser()
	{
		if($_POST){
			$this->form_validation->set_rules('name', '用户名称', 'required');
			$this->form_validation->set_rules('password', '密码', 'required|min_length[6]|max_length[12]|matches[passconf]');
			$this->form_validation->set_rules('passconf', '密码确认', 'required');
			$this->form_validation->set_rules('email', '电子邮件', 'required|valid_email|is_unique[aauth_users.email]');
			$this->form_validation->set_error_delimiters('<div class="text-red">', '</div>');
				
			if (!$this->form_validation->run() == FALSE)
			{
				$newUser = $this->aauth->create_user($_POST['email'], $_POST['password'], $_POST['name']);
				redirect('users');
			}
		}
		$data['title'] = $this->title('用户管理');
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/users/addUser');
		$this->load->view($this->theme.'/footer');

	}

	function editUser($user_id  = false)
	{
		if(!$this->aauth->get_user($user_id)){
			redirect('users');
		}else{

			if($_POST){
				$this->form_validation->set_rules('name', '用户名称', 'required');
				$this->form_validation->set_error_delimiters('<div class="text-red">', '</div>');
				if ($this->form_validation->run() == FALSE)
				{

				}
				else
				{
					$user = $this->aauth->update_user($user_id, false, false, $_POST['name']);
					$data['msg'] = '修改信息已经保存.';
				}
			}

			$data['title'] = $this->title('用户管理');
			$data['user'] = $this->aauth->get_user($user_id);
			$this->load->view($this->theme.'/header',$data);
			$this->load->view($this->theme.'/users/editUser');
			$this->load->view($this->theme.'/footer');
		}
	}


	function sendPassword($user_id = false)
	{
		if(!$user_id){
			redirect('users');
		}else{
			$user = $this->aauth->get_user($user_id);
			if(!$user){
				$data['msgError'] = "此用户ID不存在. ";
			}else{
				$this->aauth->remind_password($user->email);
				$data['user'] = $this->aauth->get_user($user_id);
				$data['msgSuccess'] = "邮件已经发送到一下信箱: ";
			}
			$data['user'] = $this->aauth->get_user($user_id);
			$data['title'] = $this->title('用户管理');
			$this->load->view($this->theme.'/header',$data);
			$this->load->view($this->theme.'/users/sendPassword');
			$this->load->view($this->theme.'/footer');
		}

	}

	
	function delete($user_id = false)
	{
		if(!$user_id){
			redirect('users');
		}else{
			$user = $this->aauth->get_user($user_id);
			if(!$user){
				redirect('users');
			}else{
				if($_POST){
					$this->aauth->delete_user($user_id);
					$data['title'] = $this->title('用户管理');
					$data['user'] = $user;
					$this->load->view($this->theme.'/header',$data);
					$this->load->view($this->theme.'/users/deleteUserResult');
					$this->load->view($this->theme.'/footer');
				}else{
				$data['title'] = $this->title('用户管理');
				$data['user'] = $user;
				$this->load->view($this->theme.'/header',$data);
				$this->load->view($this->theme.'/users/deleteUser');
				$this->load->view($this->theme.'/footer');
				}
			}
		}
	}




}